package com.hontech.kaltaapp.ui.parts

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.Button
import android.widget.FrameLayout
import com.hontech.kaltaapp.R
import com.hontech.kaltaapp.device.getParam
import com.hontech.kaltaapp.device.setParam
import com.hontech.kaltaapp.ui.dialog.runProg
import com.hontech.kaltaapp.ui.dialog.runProgMsg
import com.hontech.kaltaapp.ui.view.LabelInput
import com.hontech.kaltaapp.ui.view.LabelSpinner
import com.hontech.kaltaapp.utils.onClick

class ParamParts (context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.parts_param, this)
        val sp = view.findViewById<LabelSpinner>(R.id.id_param_sp)
        val get = view.findViewById<Button>(R.id.id_param_btn_get)
        val li = view.findViewById<LabelInput>(R.id.id_param_li_value)
        val set = view.findViewById<Button>(R.id.id_param_btn_set)
        get.onClick {
            runProgMsg("查询", sp.selectText()) {
                val v = getParam(sp.selectIndex())
                "${sp.selectText()}:${v}"
            }
        }
        set.onClick {
            runProg("设置", sp.selectText()) {
                val v = li.textInt()
                setParam(sp.selectIndex(), v)
            }
        }
    }
}